using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class MapEditorWindow : EditorWindow
{
    private static GameObject currentMapRoot;
    
    [MenuItem("Tools/地图编辑器")]
    public static void MapEditorWindowStart()
    {
        GetWindow<MapEditorWindow>("地图编辑器");
    }

    public void OnGUI()
    {
        if(!Selection.activeGameObject)
        {
            GUILayout.Label("请选择一个地图根");
            if(GUILayout.Button("新建地图根"))
            {
                currentMapRoot = new GameObject("MapRoot");
                currentMapRoot.name = "MapRoot";
                currentMapRoot.transform.position = Vector3.zero;
                Selection.activeGameObject = currentMapRoot;
            }
            return;
        }
        
        if (currentMapRoot != Selection.activeGameObject)
        {
            GUILayout.Label("选择地图根注意事项:");
            GUILayout.Label("1. 地图根必须为空物体");
            GUILayout.Label("2. 地图根不能有子物体");
            GUILayout.Label("3. 地图根的层级必须为0");
            GUILayout.Label("4. 地图根的坐标必须为(0,0,0)");
            GUILayout.Label("5. 地图根的旋转必须为(0,0,0)");
            GUILayout.Label("6. 地图根的缩放必须为(1,1,1)");
            if (GUILayout.Button($"确定选择\"{Selection.activeGameObject.name}\"为地图根"))
            {
                currentMapRoot = Selection.activeGameObject;
            }
            return;
        }
        
        GUILayout.Label($"当前选择的地图根为 : {Selection.activeGameObject.name}");
        if (GUILayout.Button("为地图根添加必要环境"))
        {
            while (currentMapRoot.transform.childCount > 0)
            {
                DestroyImmediate(currentMapRoot.transform.GetChild(0).gameObject);
            }

            MapEditorController mapEditorController = currentMapRoot.GetComponent<MapEditorController>();
            if (mapEditorController == null)
            {
                mapEditorController = currentMapRoot.AddComponent<MapEditorController>();
            }
            
            GameObject RayPanel = GameObject.CreatePrimitive(PrimitiveType.Plane);
            RayPanel.name = "RayPanel";
            RayPanel.transform.parent = currentMapRoot.transform;
            RayPanel.transform.position = new Vector3(13, 13, 0);
            RayPanel.transform.rotation = Quaternion.Euler(-90.0f, 0, 0);
            RayPanel.transform.localScale = new Vector3(2.6f, 2.6f, 2.6f);
            
            mapEditorController.MapEditorItemList.Clear();
            GameObject itemPool = new GameObject("MapItemPool");
            itemPool.transform.parent = currentMapRoot.transform;
            for(int y = 0; y < 26; ++y)
            {
                for (int x = 0; x < 26; x++)
                {
                    if (IsNoEditPoint(x, y))
                    {
                        continue;
                    }
                    
                    GameObject mapItem = new GameObject($"MapItem_({x},{y})");
                    mapItem.tag = "MapEditorItem";
                    mapItem.transform.position = new Vector3(x, y, 0.0f);
                    mapItem.transform.parent = itemPool.transform;
                    
                    mapItem.AddComponent<BoxCollider2D>().offset = new Vector2(0.5f, 0.5f);
                    
                    SpriteRenderer spriteRenderer = mapItem.AddComponent<SpriteRenderer>();
                    spriteRenderer.sprite = AssetDatabase.LoadAssetAtPath<Sprite>("Assets/GameRes/Textures/Spites/Map/MapEditorDefault.psd");

                    MapEditorItem mapEditorItem = mapItem.AddComponent<MapEditorItem>();
                    mapEditorItem.spriteRenderer = spriteRenderer;
                    mapEditorController.MapEditorItemList.Add(mapEditorItem);
                }
            }
        }
    }

    private bool IsNoEditPoint(int x, int y)
    {
        if(x is 12 or 13)
        {
            if (y is 0 or 1)
            {
                return true;
            }
        }

        if (y is 24 or 25)
        {
            if (x is 0 or 1 or 12 or 13 or 24 or 25)
            {
                return true;
            }
        }
        
        return false;
    }

    private void OnSelectionChange()
    {
        Repaint();
    }
}
